package com.ruoyi.ruoyixingyue.modules.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.ConvertUtils;
import com.ruoyi.common.core.utils.page.PageData;
import com.ruoyi.ruoyixingyue.modules.api.entity.Student;
import com.ruoyi.ruoyixingyue.modules.api.mapper.StudentMapper;
import com.ruoyi.ruoyixingyue.modules.api.po.StudentListPo;
import com.ruoyi.ruoyixingyue.modules.api.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.ruoyixingyue.modules.api.vo.StudentListVo;
import com.ruoyi.ruoyixingyue.modules.api.vo.StudentStatusVo;
import com.ruoyi.ruoyixingyue.modules.utils.Condition;
import com.ruoyi.ruoyixingyue.modules.utils.QueryPage;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 星月学生表 服务实现类
 * </p>
 *
 * @author lzz
 * @since 2024-02-18
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Override
    public R<PageData<StudentListPo>> studentListAPI_001(Condition<StudentListVo> condition) {
        StudentListVo studentListVo = condition.getCondition();
        QueryPage queryPage = condition.getPage();
        Page<Student> studentPage = new Page<Student>().setCurrent(queryPage.getCurrent()).setSize(queryPage.getSize());
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();

        //  进行查询
        List<Student> studentList = this.list(queryWrapper.lambda()
                .orderByDesc(Student::getUpdateTime)
                .like(Objects.nonNull(studentListVo.getUserName()), Student::getUserName, studentListVo.getUserName())
                .like(Objects.nonNull(studentListVo.getRealName()), Student::getRealName, studentListVo.getRealName())
                .eq(Objects.nonNull(studentListVo.getCellPhone()), Student::getCellPhone, studentListVo.getCellPhone())
                .eq(Objects.nonNull(studentListVo.getStatus()), Student::getStatus, studentListVo.getStatus()));



        if (CollectionUtils.isEmpty(studentList)) {
            return R.ok(new PageData<>(new ArrayList<StudentListPo>(),0));
        }

        List<StudentListPo> resultList = new ArrayList<>();
        this.page(studentPage,queryWrapper);
        studentList.forEach(student -> {
            StudentListPo target = ConvertUtils.sourceToTarget(student, StudentListPo.class);
            resultList.add(target);
        });
        return R.ok(new PageData<>(resultList,studentPage.getTotal()));
    }

    @Override
    public R<String> studentUpdateStatusAPI_002(StudentStatusVo studentStatusVo) {
        Long id = studentStatusVo.getId();
        Integer status = studentStatusVo.getStatus();
        Student student = this.getById(id);
        student.setStatus(status);
        this.updateById(student);
        return R.ok(null,"修改成功");
    }
}
